package cst.a722wr.zyfypt_722wr.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import cst.a722wr.zyfypt_722wr.Bean.LoginBean;
import cst.a722wr.zyfypt_722wr.R;
import cst.a722wr.zyfypt_722wr.iface.wrListener;
import cst.a722wr.zyfypt_722wr.model.ResModel;


public class LoginActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;
    private Button btn_login;
    private Button btn_reg;
    private Switch aSwitch;
    private ResModel model;
    private wrListener<LoginBean> listener;
    private Intent intent;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (EditText) findViewById(R.id.login_username);
        password = (EditText) findViewById(R.id.login_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_reg = (Button) findViewById(R.id.btn_reg);
        aSwitch = (Switch) findViewById(R.id.login_switch);

        sp = getSharedPreferences("ME", MODE_PRIVATE);
        editor = sp.edit();
        if(sp.getString("aswitch","").equals("true")){
            aSwitch.setChecked(true);
            username.setText(sp.getString("username",""));
            password.setText(sp.getString("password",""));
            username.setSelection(username.getText().length());
            password.setSelection(password.getText().length());
        }else{
            aSwitch.setChecked(false);
        }

        listener = new wrListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean loginBean) {
                if(loginBean.getUsername() != null){
                    String sessionid = loginBean.getSessionid();
                    editor.putString("username", loginBean.getUsername());
                    editor.putString("password", password.getText().toString());
                    editor.putString("session_id", sessionid);
                    if(aSwitch.isChecked()){
                        editor.putString("aswitch","true");
                    }else{
                        editor.putString("aswitch","false");
                    }
                    editor.commit();
                    intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!username.getText().toString().isEmpty()){
                    model = new ResModel("gson");
                    model.login(username.getText().toString(), password.getText().toString(), listener);
                }
                else{
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
